package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.salesmanage.util.SaleOrder;
import com.turing.post.salesmanage.util.SaleOrderDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 规则： 同一经办人下使用不同的单位名称进行购买的
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule1")
public class AlarmRule1 implements AlarmRulesBusiness {

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao personalCustomDaoImpl;
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;

	@EJB(mappedName = "salesmanage.impl.SaleOrderDaoImpl")
	private SaleOrderDao saleOrderDaoImpl;

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {
		// 查询今天saleOrder中为单位购卡的所有代理人列表
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issueCompanyNo);
		List<Object> proxybuyerList = saleOrderDaoImpl
				.getProxybuyerListForAlarm(PCPSUtil
						.getPackedYMDString2(issueCompany.getTranDate()));
		// 查询存在风险的销售单
		BigInteger proxybuyerid = null;
		Integer count = 0;
		List<SaleOrder> alarmOrders = null;
		PersonalCustom proxyPersonalCustom = null;
		for (Object objs : proxybuyerList) {
			proxybuyerid = new BigInteger(objs.toString());
			count = saleOrderDaoImpl.getProxybuyerSaleOrderNum(proxybuyerid);
			if (count > 1) {// 存在风险交易
				alarmOrders = saleOrderDaoImpl
						.getProxyBuyerSaleOrderList(proxybuyerid);
				proxyPersonalCustom = personalCustomDaoImpl
						.getPersonalCustomByPersonalId(proxybuyerid);
				releaseAlarm(alarmOrders, issueCompany, alarmRule, tranDate,
						proxyPersonalCustom);// 发布风险信息
			}
		}
	}

	/**
	 * 发布风险信息
	 * 
	 * @param tranDate
	 * @param alarmRule
	 * @param issueCompany
	 * 
	 * @param alarmOrders存在风险的销售单
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(List<SaleOrder> alarmOrders,
			IssueCompany issueCompany, AlarmRule alarmRule, Date tranDate,
			PersonalCustom proxyPersonalCustom) throws PCPSApplicationException {
		AlarmInfo alarmInfo = new AlarmInfo();
		SaleOrder lastSaleOrder = alarmOrders.get(0);
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(lastSaleOrder.getSaleBranchNo());
		alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
		alarmInfo.setIssueCompanyNo(issueCompany.getIssueCompanyNo());
		alarmInfo.setBranchNo(lastSaleOrder.getSaleBranchNo());
		alarmInfo.setBranchName(saleBranch.getSaleBranchName());
		alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
		alarmInfo.setRiskCustomName(proxyPersonalCustom.getName());
		alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
		alarmInfo.setDealStatus("0");

		BigInteger alarmInfoId = alarmInfoDaoImpl.getMaxAlarmInfoId();
		alarmInfo.setAlarmInfoId(alarmInfoId);
		// 插入预警信息
		alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);
		for (int i = 1; i <= alarmOrders.size(); i++) {
			SaleOrder order = alarmOrders.get(i - 1);
			AlarmInfoDetail alarmInfoDetail = new AlarmInfoDetail();
			alarmInfoDetail.setAlarmInfoId(alarmInfoId);
			alarmInfoDetail.setSeqNo(i);
			alarmInfoDetail.setBusinessType("0");
			alarmInfoDetail.setBusinessOrderId(order.getSaleOrderId());
			alarmInfoDetail.setTranDate(tranDate);
			alarmInfoDetailDaoImpl.insertAlarmInfoDetail(alarmInfoDetail);
		}
	}
}
